home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / PowerFantasm™ 4.19a / PowerFantasm™ / F4_EXAMPLES / Other people examples / Stars / stars.s < prev   
Text File  |  1996-12-04  |  7KB  |  282 lines

  1. stars_demo:
  2. *********************************************************************************
  3. ** Stars
  4. ** by Allan Jensen (maccoder@geocities.com)
  5. ** Further info: Read the doc
  6. ********************************************************************************
  7. **Set up equates
  8. n_stars:        equ        1200            *can easily handle more (too many requires some alterations)
  9. dim_one:        equ        6*n_stars        *3d coords for stars (halfword)
  10. black:            equ        0                *definition black
  11. screenx:        equ        512
  12. screeny:        equ        400
  13. speed:            equ        2                *starspeed
  14. zoom:            equ        128                *how close the stars
  15. z_const:        equ        600
  16. *bss section
  17. bss:            reg        r30
  18. res_clut:        rs.w    1
  19. handle_one:        rs.w    1
  20. pointer_one:    rs.w    1
  21. screen_buffer:    rs.w    1                *Where our offscreen screen buffer starts
  22. rect_top:         rs.h    1
  23. rect_left:        rs.h    1
  24. rect_bottom:    rs.h    1
  25. rect_right:        rs.h    1
  26. *********************************************************************************        
  27.         ENTRY
  28.         start_up
  29.         bl        init_mac
  30.         bl        init_graph_lib
  31.         Xcall    HideCursor
  32.  
  33.         li        r3,dim_one
  34.         Xcall    NewHandle
  35.         stw        r3,handle_one(`bss)
  36.         mr        r20,r3
  37.         Xcall    HLock
  38.         mr        r3,r20
  39.         lwz        r3,(r3)
  40.         stw        r3,pointer_one(`bss)
  41.  
  42.         li        r3,screenx*2        *width
  43.         li        r4,screeny+2*64        *height
  44.         mullw    r3,r3,r4            *size we need
  45.         addi    r3,r3,10*(2*screenx)    *plus a bit just in case
  46.         Xcall    NewHandle
  47.         mr        r20,r3                *save handle
  48.         Xcall    HLock                *We want a locked handle so we can use the pointer
  49.         lwz        r20,(r20)            *the pointer to the data
  50. check_octal:
  51.         mr        r3,r20
  52.         andi.    r3,r3,%111            *and with 7
  53.         beq        is_aligned
  54.         addi    r20,r20,1
  55.         b        check_octal
  56. is_aligned:
  57.         stw        r20,screen_buffer(`bss)
  58.  
  59.         li        r3,130
  60.         bl        load_clut
  61.         stw        r3,res_clut(`bss)
  62.  
  63.         lwz        r3,res_clut(`bss)
  64.         lwz        r3,(r3)
  65.         bl        do_clut                *remember to use a white to black fading palette
  66.  
  67.         li        r3,black
  68.         bl        cls_256
  69.  
  70.         bl        calc_rect            *screen setup stuff (by Lightsoft, wahey!)
  71.         bl        make_table            *randomize coordinates
  72. fire_loop:
  73.         bl        clear_offscreen        *clear by ....Lightsoft...again
  74.         bl        do_stars            *convert to 2d, move the star and plot it
  75.         bl        splat_screen        *another routine by guess who
  76.         bl        getkey_turbo
  77.         cmpwi    r3,0
  78.         beq        fire_loop
  79. QUIT:     
  80.         li        r3,black
  81.         bl        cls_256
  82.         lwz        r3,handle_one(`bss)
  83.         Xcall    HUnlock
  84.         Xcall    ShowCursor
  85.         tidy_up
  86.         blr
  87.  
  88. ****************** Routines ********************
  89. **Calculate viewable rectangle from screen size by Lightsoft
  90. calc_rect:
  91.         mflr    r29
  92.         lwz        r3,vis_height(rtoc)    *visible screen height
  93.         srwi    r3,r3,1                *middle of screen
  94.         subi    r3,r3,screeny/2        *top of viewrect
  95.         sth        r3,rect_top(`bss)
  96.         lwz        r3,vis_width(rtoc)
  97.         srwi    r3,r3,1
  98.         subi    r3,r3,(screenx/2)-8
  99. **make sure its a multiple of 8
  100.         mr         r4,r3
  101. make_x_8:
  102.         mr        r3,r4
  103.         andi    r3,r3,%111
  104.         beq        is_8
  105.         subi    r4,r4,1
  106.         b        make_x_8
  107. is_8:    
  108.         sth        r4,rect_left(`bss)
  109.         mtlr    r29
  110.         blr
  111.  
  112. *This routine calculates a table full of coordinates (x,y,z)
  113. *x is a random number between 0 and screen width (screenx)
  114. *y is a random number between 0 and screen height (screeny)
  115. *z is a random number and so on and so on and so on and......
  116. *Remember that coords must lie within screen (well, obviously)
  117. make_table:
  118.         mflr    r29
  119.         lwz        r14,pointer_one(`bss)
  120.         li        r9,n_stars
  121. table_loop:
  122.         Xcall    Random
  123.         andi    r3,r3,$fff
  124.         cmpwi    r3,screenx-1
  125.         bgt        table_loop
  126.         sth        r3,(r14)
  127.         addi    r14,r14,2
  128. y_calc::
  129.         Xcall    Random
  130.         andi    r3,r3,$fff
  131.         cmpwi    r3,screeny-1
  132.         bgt        y_calc
  133.         sth        r3,(r14)
  134.         addi    r14,r14,2
  135. z_calc:
  136.         Xcall    Random            *experiment with the z-values to decide what you like best
  137.         andi    r3,r3,$fff
  138.         cmpwi    r3,z_const
  139.         bgt        z_calc
  140.         sth        r3,(r14)        
  141.         addi    r14,r14,2
  142.         subi    r9,r9,1
  143.         cmpwi    r9,0
  144.         bgt        table_loop
  145.         mtlr    r29
  146.         blr
  147.  
  148. *Converts the 3d coords to 2d, moves the star and plots it
  149. *Uses formula 3d->2d:
  150. *x1 = (x*zoomconst)/z
  151. *y1 = (y*zoomconst)/z
  152. *Extra bits explained in doc (it's a long story)
  153. do_stars:
  154.         mflr    r29
  155.         lwz        r10,pointer_one(`bss)
  156.         lwz        r6,screen_buffer(`bss)
  157.         li        r9,n_stars
  158. 2d_loop:
  159.         lhz        r3,(r10)        *first we get the coords
  160.         addi    r10,r10,2
  161.         lhz        r4,(r10)
  162.         addi    r10,r10,2
  163.         lhz        r5,(r10)
  164.  
  165.         subi    r3,r3,screenx/2    *the we convert them to 2d
  166.         slwi    r3,r3,7            *instead of muls with zoom
  167.         divw    r3,r3,r5
  168.         addi    r3,r3,screenx/2
  169.  
  170.         subi    r4,r4,screeny/2
  171.         slwi    r4,r4,7
  172.         divw    r4,r4,r5
  173.         addi    r4,r4,screeny/2
  174.  
  175.         cmpwi    r5,speed+1        *let's check if the star expires
  176.         bgt        closer
  177.         li        r7,z_const        *put it far away
  178.         sth        r7,(r10)
  179.         subi    r10,r10,4
  180.         b        2d_loop
  181. closer:
  182.         subi    r5,r5,speed        *let's move the star closer
  183.         sth        r5,(r10)
  184.         addi    r10,r10,2
  185.  
  186.         cmpwi    r3,screenx-1    *some bounds checking
  187.         bgt        next_star
  188.         cmpwi    r3,0
  189.         blt        next_star
  190.         cmpwi    r4,screeny-1
  191.         bgt        next_star
  192.         cmpwi    r4,0
  193.         blt        next_star
  194.         li        r12,3            *futile attempt to make colours look nice :)
  195.         divw    r5,r5,r12
  196.                                 *plot the star
  197.         slwi    r4,r4,10        *instead of mulli with screenx
  198.         add        r4,r4,r6
  199.         add        r4,r4,r3
  200.         stb        r5,(r4)        
  201. next_star:
  202.         subi    r9,r9,1
  203.         cmpwi    r9,0
  204.         bgt        2d_loop
  205.         mtlr    r29
  206.         blr
  207.  
  208. *Lightsoft...again
  209. clear_offscreen:
  210.         li        r3,screenx*2
  211.         li        r4,screenx-1        *height
  212.         mullw    r3,r3,r4
  213.         srwi    r3,r3,2                *div by 4 for longs
  214.         mtctr    r3
  215.         lwz        r3,screen_buffer(`bss)
  216.         li        r4,0
  217. clear_loop:
  218.         stw        r4,(r3)
  219.         addi    r3,r3,4
  220.         bdnz    clear_loop
  221.         blr
  222.  
  223. *and again....
  224. splat_screen:
  225. **Each line is 125 longs, and there are 400 lines. The addresses we get from y_tab
  226.         lwz        r3,y_tab(rtoc)        *address of the start of each scan line
  227.         lhz        r4,rect_top(`bss)
  228.         slwi    r4,r4,2
  229.         add        r3,r3,r4            *Offset splat in y direction*4        
  230.         li        r8,screeny            *400 lines per screen
  231.         lwz        r6,screen_buffer(`bss)     *source data
  232.         subi    r6,r6,8                *for pre-inc addressing mode
  233.         li        r14,2*screenx
  234.         li        r7,64
  235.         mullw    r7,r7,r14
  236.         add        r6,r6,r7
  237.         lhz        r22,rect_left(`bss)    *get x offset
  238.         li        r4,(screenx/4)/2    *124 longs/line
  239.         addi    r6,r6,32            *move right 32
  240. **Offset offsceen by 32
  241.         subi    r3,r3,4
  242. splat_loop:
  243.         mtctr    r4
  244.         lwzu    r5,4(r3)            *start addr of this scan line
  245.         add        r5,r5,r22            *offset splat in x direction
  246. line_loop:
  247.         lfdu    f0,8(r6)
  248.         stfdu    f0,8(r5)
  249.         bdnz    line_loop            *complete the line to 546
  250.         subic.    r8,r8,1
  251.         addi    r6,r6,(2*screenx-screenx+32)+32    *point to next line
  252.         bne        splat_loop            *do next line
  253.         blr
  254.  
  255. load_clut:
  256.         mflr    r28
  257.         mr        r4,r3
  258.         li        r3,"cl"
  259.         li        r5,16
  260.         slw        r3,r3,r5
  261.         li        r5,"ut"
  262.         or        r3,r3,r5
  263.         Xcall    GetResource
  264.         mtlr    r28
  265.         blr
  266.  
  267. do_clut:           
  268.         mflr    r29
  269.         mr        r5,r3
  270.         li        r3,0
  271.         li        r4,255
  272.         Xcall    SetEntries
  273.         mtlr    r29
  274.         blr
  275.  
  276. *******************************************************************************************
  277.  
  278.         global    stars_demo
  279.         extern    init_graph_lib,init_mac,cls_256,getkey_turbo
  280.         extern_data    y_tab,screen_base
  281.         extern_data vis_width,vis_height    
  282.